package efr.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import efr.dao.RimaDao;
import efr.dao.impl.RimaDaoImpl;
import efr.entity.Rima;

/**
 * Servlet implementation class Op4RimaServlet
 */
public class Op4RimaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Op4RimaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String nextPage = null;
		if ("Modifica".equals(request.getParameter("Modifica"))) {
			String id = request.getParameter("id");
			RimaDao rimaDao = new RimaDaoImpl();
			Rima rima = rimaDao.getRima(Integer.valueOf(id));
			request.setAttribute("rima", rima);
			request.setAttribute("action", "Modifica");
			nextPage = "/formRima.jsp";
		}
		if ("Sterge".equals(request.getParameter("Sterge"))) {
			String id = request.getParameter("id");
			RimaDao rimaDao = new RimaDaoImpl();
			Rima rima = rimaDao.getRima(Integer.valueOf(id));
			rimaDao.deleteRima(rima);
			nextPage = "/ListRime.do";
		}
		if ("Adauga".equals(request.getParameter("Adauga"))) {
			request.setAttribute("action", "Adauga");
			nextPage = "/formRima.jsp";
		}
		RequestDispatcher view = request.getRequestDispatcher(nextPage);
		view.forward(request, response);
	}

}
